var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var in_task = require("./inTask");
//搜一搜赚钱 2次
var search_2 = {
  is_over_today: false,
  is_first_back: true,
  go_page_from_home() {
    var logger = utils.create_logger("search_2 go_page_from_home: ");
    if (ocr_utils.screen_exists_text_all(["首页", "发现"])) {
      logger("");
      // utils.click_point.apply(
      //   utils,
      //   utils.get_size_point(point_map["首页任务"])
      // );
      if (this.is_over_today) {
        logger("今天的--search2--任务全部完成了");
        return;
      }
      sleep(2 * 1000);
      this.init();
    } else {
      logger("NOT FOUND");
    }
  },
  init() {
    var logger = utils.create_logger("search_2 init: ");
    in_task.init();
    sleep(utils.get_random_interval(2, 0, 2, 1000));
    var ocr_text_results = ocr_utils.find_text_all(
      ["搜一搜赚钱"],
      false,
      null,
      5
    );
    if (ocr_text_results.length > 0) {
      logger("ocr_text_result:", ocr_text_results);
      var ocr_text_result = ocr_text_results.find(
        (x) => x.text == "搜一搜赚钱"
      );
      if (ocr_text_result) {
        var ocr_text = ocr_text_result.result;
        var x = ocr_text.bounds.centerX();
        var y = ocr_text.bounds.centerY();
        logger("找到【搜一搜赚钱】", "坐标：x: " + x + " y: " + y);
        this.run(x, y);
      } else {
        logger("没找到 1【搜一搜赚钱】");
      }
    } else {
      logger("没找到 2【搜一搜赚钱】");
    }

    console.hide();
    sleep(1 * 1000);
  },
  run(x, y) {
    for (var index = 0; index < 2; index++) {
      utils.click_x_y(x, y);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      utils.click_point.apply(
        utils,
        utils.get_size_point([871, 141, 1025, 212], 1080, 2340)
      );

      sleep(utils.get_random_interval(2, 0, 2, 1000));

      var search_swipe_times = 5 + random(0, 2);
      console.log(
        "search_2 run: 即将开始第" +
          (index + 1) +
          "次搜索，需要滑动--" +
          search_swipe_times +
          "--次"
      );
      while (search_swipe_times > 0) {
        utils.swipe_x(
          device.width / 2,
          (device.height / 10) * 5,
          device.width / 2,
          (device.height / 10) * 2,
          utils.get_random_interval(300, 0, 700, 1)
        );
        sleep(utils.get_random_interval(3, 0, 2, 1000));
        search_swipe_times = search_swipe_times - 1;
        if (search_swipe_times == 0) {
          console.log("search_2 run: 第" + (index + 1) + "次搜索已完成");
        } else {
          console.log("search_2 run: 还需要滑动" + search_swipe_times + "次");
        }
      }
      back();
      sleep(3 * 1000);
      if (utils.click_text_starts_with("残忍离开")) {
        sleep(2 * 1000);
        console.log("search_2 找到--残忍离开--");
      } else {
        console.log("search_2 没找到--残忍离开--");
      }
    }
    this.is_over_today = true;
    console.log("search2 run: 今天的--search2--任务全部完成了");
    this.after();
  },
  after() {
    back();
    sleep(2 * 1000);
    in_task.init();
    console.log("search_2 after");
  },
  next() {
    console.log("search_2 next");
  },
};
module.exports = search_2;
